home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / libdes / pcbc_enc.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  4KB  |  116 lines

  1. /* lib/des/pcbc_enc.c */
  2. /* Copyright (C) 1995 Eric Young (eay@mincom.oz.au)
  3.  * All rights reserved.
  4.  * 
  5.  * This file is part of an SSL implementation written
  6.  * by Eric Young (eay@mincom.oz.au).
  7.  * The implementation was written so as to conform with Netscapes SSL
  8.  * specification.  This library and applications are
  9.  * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
  10.  * as long as the following conditions are aheared to.
  11.  * 
  12.  * Copyright remains Eric Young's, and as such any Copyright notices in
  13.  * the code are not to be removed.  If this code is used in a product,
  14.  * Eric Young should be given attribution as the author of the parts used.
  15.  * This can be in the form of a textual message at program startup or
  16.  * in documentation (online or textual) provided with the package.
  17.  * 
  18.  * Redistribution and use in source and binary forms, with or without
  19.  * modification, are permitted provided that the following conditions
  20.  * are met:
  21.  * 1. Redistributions of source code must retain the copyright
  22.  *    notice, this list of conditions and the following disclaimer.
  23.  * 2. Redistributions in binary form must reproduce the above copyright
  24.  *    notice, this list of conditions and the following disclaimer in the
  25.  *    documentation and/or other materials provided with the distribution.
  26.  * 3. All advertising materials mentioning features or use of this software
  27.  *    must display the following acknowledgement:
  28.  *    This product includes software developed by Eric Young (eay@mincom.oz.au)
  29.  * 
  30.  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  31.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  34.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  39.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  40.  * SUCH DAMAGE.
  41.  * 
  42.  * The licence and distribution terms for any publically available version or
  43.  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  44.  * copied and put under another distribution licence
  45.  * [including the GNU Public Licence.]
  46.  */
  47.  
  48. #include "des_locl.h"
  49.  
  50. void des_pcbc_encrypt(input, output, length, schedule, ivec, encrypt)
  51. des_cblock (*input);
  52. des_cblock (*output);
  53. long length;
  54. des_key_schedule schedule;
  55. des_cblock (*ivec);
  56. int encrypt;
  57.     {
  58.     register unsigned long sin0,sin1,xor0,xor1,tout0,tout1;
  59.     unsigned long tin[2];
  60.     unsigned char *in,*out,*iv;
  61.  
  62.     in=(unsigned char *)input;
  63.     out=(unsigned char *)output;
  64.     iv=(unsigned char *)ivec;
  65.  
  66.     if (encrypt)
  67.         {
  68.         c2l(iv,xor0);
  69.         c2l(iv,xor1);
  70.         for (; length>0; length-=8)
  71.             {
  72.             if (length >= 8)
  73.                 {
  74.                 c2l(in,sin0);
  75.                 c2l(in,sin1);
  76.                 }
  77.             else
  78.                 c2ln(in,sin0,sin1,length);
  79.             tin[0]=sin0^xor0;
  80.             tin[1]=sin1^xor1;
  81.             des_encrypt((unsigned long *)tin,schedule,DES_ENCRYPT);
  82.             tout0=tin[0];
  83.             tout1=tin[1];
  84.             xor0=sin0^tout0;
  85.             xor1=sin1^tout1;
  86.             l2c(tout0,out);
  87.             l2c(tout1,out);
  88.             }
  89.         }
  90.     else
  91.         {
  92.         c2l(iv,xor0); c2l(iv,xor1);
  93.         for (; length>0; length-=8)
  94.             {
  95.             c2l(in,sin0);
  96.             c2l(in,sin1);
  97.             tin[0]=sin0;
  98.             tin[1]=sin1;
  99.             des_encrypt((unsigned long *)tin,schedule,DES_DECRYPT);
  100.             tout0=tin[0]^xor0;
  101.             tout1=tin[1]^xor1;
  102.             if (length >= 8)
  103.                 {
  104.                 l2c(tout0,out);
  105.                 l2c(tout1,out);
  106.                 }
  107.             else
  108.                 l2cn(tout0,tout1,out,length);
  109.             xor0=tout0^sin0;
  110.             xor1=tout1^sin1;
  111.             }
  112.         }
  113.     tin[0]=tin[1]=0;
  114.     sin0=sin1=xor0=xor1=tout0=tout1=0;
  115.     }
  116.